package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Dep;
import cn.itcast.erp.entity.Role;
import cn.itcast.erp.entity.Tree;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.entity.utils.ResultAjax;
import cn.itcast.erp.service.RoleService;
import org.apache.commons.io.IOUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/role")

public class RoleController {

    @Resource
    private RoleService roleService;


    @CheckPower("角色设置")
    @RequestMapping("/find")
    public AjaxResult findRole(Role role){

        return this.roleService.readRoleMenus(role);
    }

    @CheckPower("角色设置")
    @RequestMapping("/findAll")
    public List<Role> findRoleAll(Role role){

        return this.roleService.readRoleMenusAll(role);
    }

    @CheckPower("角色设置")
    @RequestMapping("/update")
    public ResultAjax update(Role role){
        ResultAjax resultAjax = new ResultAjax();
        int i = roleService.updateRole(role);
        if (i > 0){
            resultAjax.setMessage("修改成功!!");
            return resultAjax;
        }
        else {
            resultAjax.setMessage("修改失败!!");
            return resultAjax;
        }
    }
    @CheckPower("角色设置")
    @RequestMapping("/delete")
    public ResultAjax delete(Role role){
        ResultAjax resultAjax = new ResultAjax();
        int i = roleService.deleteRole(role);
        if (i > 0){
            resultAjax.setMessage("成功删除!!");
            return resultAjax;
        }
        else {
            resultAjax.setMessage("删除失败!!");
            return resultAjax;
        }
    }

    @CheckPower("角色设置")
    @RequestMapping("/insert")
    public ResultAjax insertRole(Role role){
        ResultAjax resultAjax = new ResultAjax();
        int i = roleService.insertRole(role);
        if (i > 0){
            resultAjax.setMessage("成功添加!!");
            return resultAjax;
        }
        else {
            resultAjax.setMessage("添加失败!!");
            return resultAjax;
        }
    }


    @CheckPower("角色设置")
    @RequestMapping("/findById")
    public Role findRoleById(Long uuid){
        return this.roleService.findRoleById (uuid);
    }

    // 查询用户打钩的角色
    @CheckPower("角色设置")
    @RequestMapping("/findRoles")
    public List<Tree> findRoles(Long id){

        return roleService.findRoles(id);

    }


    // 更改用户的角色
    @CheckPower("用户角色设置")
    @RequestMapping("/updateEmpRoles")
    public ResultAjax updateEmpRoles(Long id, String checkedStr){
        ResultAjax resultAjax = new ResultAjax();
        int i = roleService.updateEmpRoles(id, checkedStr);
        if (i > 0){
            resultAjax.setMessage("修改成功!!");
        }else {
            resultAjax.setMessage("修改失败!!");
        }
        return resultAjax;

    }


    //导出Excel
    @CheckPower("角色设置")
    @RequestMapping("/export")
    public void getAllRoleEx(HttpServletResponse response){
        try {
            response.reset();
            response.setContentType("application/msexcel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("角色设置信息.xls", "UTF-8"));
            InputStream inputStream = roleService.getAllRoleEx();
            IOUtils.copy(inputStream,response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
